/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   Main.cpp. Created on: 13.10.2010 14:36:29 
*/

#include <cpputils/CppUtils.hpp>
#include <smartobjects/SmartBus.hpp>

#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>

#include "server/ChatServiceImpl.hpp"
#include "client/ChatClientImpl.hpp"

using namespace smartobjects;


void startServer(boost::program_options::variables_map vm) {
	int nServerPort = 3333;
	if ( vm.count("port") ) {
		nServerPort = vm["port"].as<int>();
	}

	boost::intrusive_ptr < ISmartBus > poBus ( new SmartBus(), false );
	boost::intrusive_ptr < IEndpointLocation > poLocation (
			poBus->createEndpointLocation ( "tcp://localhost:"+boost::lexical_cast<std::string>(nServerPort) ),
			false
	);


	std::cout << "Starting service..." << std::endl;

	boost::intrusive_ptr < ChatServiceImpl > poChatServiceImpl ( new ChatServiceImpl( poBus.get() ) , false );
	boost::intrusive_ptr < ChatServiceSyncSkeleton > poChatServiceSkeleton (
			new ChatServiceSyncSkeleton ( poChatServiceImpl.get() ), false
	);

	std::string sReference;
	poLocation->toStringReference(sReference);

	std::cout << "Publishing on " << sReference <<  "..." << std::endl;
	poChatServiceSkeleton->publish(poBus.get(), poLocation.get());
	std::cout << "Started! Please enter something to stop." << std::endl;
	std::cin.get();
	std::cout << "Exiting..." << std::endl;
}

void startClient(boost::program_options::variables_map vm) {
	std::cout << "Starting client..." << std::endl;

	std::string sLocation = "tcp://localhost:3333";
	std::string sLogin = "John";
	std::string sPassword = "secret";
	if ( vm.count("location") ) {
		sLocation = vm["location"].as<std::string>();
	}
	if ( vm.count("login") ) {
		sLogin = vm["login"].as<std::string>();
	}
	if ( vm.count("password") ) {
		sPassword = vm["password"].as<std::string>();
	}

	boost::intrusive_ptr < ISmartBus > poBus ( new SmartBus(), false );
	boost::intrusive_ptr < IEndpointLocation > poLocation (
			poBus->createEndpointLocation ( sLocation ),
			false
	);

	boost::intrusive_ptr < IChatService > poService ( new ChatService() );

	std::string sReference;
	poLocation->toStringReference(sReference);
	std::cout << "Connecting to " << sReference <<  "..." << std::endl;

	poService->resolve(poBus.get(), poLocation.get());

	boost::intrusive_ptr < ChatClientImpl > poChatClientImpl ( new ChatClientImpl( poService.get(), sLogin, sPassword ) , false );
	poChatClientImpl->start();
	std::cout << "Exiting..." << std::endl;
}


int main(int argc, char* argv[] ) {

	// Declare the supported options.
	boost::program_options::options_description desc("Allowed options");
	desc.add_options()
	    ("help", "produce help message")
	    ("server", "Server mode")
	    ("client", "Client mode")
	    ("port", boost::program_options::value<int>(), "Server TCP port")
	    ("location", boost::program_options::value<std::string>(), "Client connect location URL")
	    ("login", boost::program_options::value<std::string>(), "Client login")
	    ("password", boost::program_options::value<std::string>(), "Client password")
	;

	boost::program_options::variables_map vm;
	boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
	boost::program_options::notify(vm);

	if (vm.count("help") || argc <2 ) {
	    std::cout << desc << "\n";
	    return 1;
	}

	try {
		if (vm.count("client")) {
			std::cout << "Chat client is loading..." << std::endl;
			startClient(vm);
		}
		else
		if ( vm.count("server") ) {
			std::cout << "Chat server is loading..." << std::endl;
			startServer(vm);
		}
		else {
			std::cerr << "Error! Working mode is not present! " << std::endl;
		}
	}
	catch(const cpputils::SystemException& ex) {
		std::cout << ex.what() << std::endl;
		ex.printBacktrace();
	}

	return 0;
}
